package classtrain;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @DESCRIPATION
 * @AUTHOR 戴超
 * @DATE 2018/6/19
 **/
public class RecImageScoketTest {
    public static void main(String[] args) throws Exception {
        Socket client = null; //  声明Socket 对象
        client = new Socket("localhost", 8888); //  指定连接的主机和端口
        InputStream in = client.getInputStream();
        File file = new File("./copy_image.jpg");
        if (file.exists()) {
            file.delete();
        }
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        int index = 0;
        byte[] bytes = new byte[1024];
        System.out.println("开始传输");
        while ((index = in.available()) >0) {
            in.read(bytes);
            out.write(bytes); //  输出信息
        }
        System.out.println("传输完成");
        out.flush();
        client.close(); //  关闭Socket
        out.close();
        in.close();
    }
}
